package com.you.concurrent.juc.cyclicBarrier;

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {

    public static void main(String[] args) throws InterruptedException {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        for (int i = 0; i < 3; i++) {
            final int index = i;
            new Thread(() -> {
                try {
                    Thread.sleep(1000L * index);
                    cyclicBarrier.await();
                    Thread.sleep(1000L * index);
                    System.out.println(System.currentTimeMillis() + " " + Thread.currentThread().getName() +
                            "----finish---");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
        Thread.sleep(1001L);
        //cyclicBarrier.reset();
        for (int i = 0; i < 3; i++) {
            final int index = i;
            new Thread(() -> {
                try {
                    Thread.sleep(1000L * index);
                    cyclicBarrier.await();
                    Thread.sleep(1000L * index);
                    System.out.println(System.currentTimeMillis() + " " + Thread.currentThread().getName() +
                            "----finish---");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
